<template>
  <div class="goods-image">
    <div class="middle" ref="target">
      <img :src="imageList.mainPictures[curIndex]" alt="" />
      <!-- 蒙层容器 -->
      <div
        v-show="isShow"
        class="layer"
        :style="{ left: left + 'px', top: top + 'px' }"
      ></div>
    </div>
    <!-- xiaotu -->
    <ul class="small">
      <li
        v-for="(img, i) in imageList.mainPictures"
        @mouseenter="curIndex = i"
        :key="i"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
    <div
      class="large"
      v-show="isShow"
      :style="[
        {
          backgroundImage: `url(${imageList.mainPictures[curIndex]})`,
        },
        bgPosition,
      ]"
    ></div>
  </div>
</template>
<script>
import { ref, watch, reactive } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    imageList: {
      type: Array,
      defalut: () => []
      // 上面的写法如果大括号里只有一行代码可以省略大括号
      // defalut: () => {
      //   return []
      // }
    }
  },
  setup () {
    const isShow = ref(false)
    const curIndex = ref(0)
    const target = ref(null)
    // elementX 鼠标基于容器左上角X轴偏移
    // elementY 鼠标基于容器左上角Y轴偏移
    // isOutside 鼠标是否在模板容器外
    // 三个参数都是ref对象
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    const left = ref(0) // 滑块距离左侧的距离
    const top = ref(0) // 滑块距离顶部的距离
    const bgPosition = reactive({ backgroundPositionX: 0, backgroundPositionY: 0 })
    watch([elementX, elementY, isOutside], () => {
      // isOutside表示是否离开 true为离开
      isShow.value = !isOutside.value
      // 监听处理
      if (isOutside) {
        // 横向移动
        if (elementX.value < 100) {
          left.value = 0
        } else if (elementX.value > 300) {
          // 右侧最大距离
          left.value = 200
        } else {
          // 有效可移动距离下的换算关系
          left.value = elementX.value - 100
        }

        // 纵向移动
        if (elementY.value < 100) {
          top.value = 0
        } else if (elementY.value > 300) {
          top.value = 200
        } else {
          top.value = elementY.value - 100
        }
        // 设置大图背景移动
        bgPosition.backgroundPositionX = -left.value * 2 + 'px'
        // console.log('da', bgPosition.backgroundPositionX)
        bgPosition.backgroundPositionY = -top.value * 2 + 'px'
      }
    })

    return { curIndex, target, left, top, bgPosition, isShow }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
.large {
  position: absolute;
  top: 0;
  z-index: 401;
  left: 412px;
  width: 400px;
  height: 400px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-repeat: no-repeat;
  background-size: 800px 800px;
  background-color: #f8f8f8;
}
.layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
</style>
